package com.stox.ui.javafx.charting.widget.menu.item;

import java.util.Collection;
import java.util.Locale;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.MenuItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.stox.dto.AbstractModel;
import com.stox.dto.intf.AbstractModelCollectionProvider;
import com.stox.event.AbstractModelSelectionEvent;

@Component
@SuppressWarnings("rawtypes")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ShowChartsMenuItem extends MenuItem implements EventHandler<ActionEvent>{

	private ApplicationContext applicationContext;
	
	private AbstractModelCollectionProvider<? extends Collection<AbstractModel>> abstractModelCollectionProvider;
	
	public ShowChartsMenuItem() {
		setOnAction(this);
	}
	
	@Autowired
	public void setApplicationContext(ApplicationContext applicationContext){
		this.applicationContext = applicationContext;
		setText(applicationContext.getMessage("Show.Chart", null, Locale.getDefault()));
	}
	
	@Override
	public void handle(ActionEvent event) {
		if(null == abstractModelCollectionProvider) return;
   		applicationContext.publishEvent(new AbstractModelSelectionEvent(abstractModelCollectionProvider));
	}

	public AbstractModelCollectionProvider getAbstractModelCollectionProvider() {
		return abstractModelCollectionProvider;
	}

	public void setAbstractModelCollectionProvider(
			AbstractModelCollectionProvider<? extends Collection<AbstractModel>> abstractModelCollectionProvider) {
		this.abstractModelCollectionProvider = abstractModelCollectionProvider;
	}
	
}
